  $(function(){
  	//初始化
	$.post("proCategory.action",function(data){
    	$("#category").append(data);
    	$("#s_category").append(data);
    });
	//输入提示
	$("#item").poshytip({
		content: 'Enter the item name,less than 50 words',
		className: 'tip-yellowsimple',
		showOn: 'focus',
		alignTo: 'target',
		alignX: 'inner-left',
		offsetX: 0,
		offsetY: 5
	});
	$("#form1 input:not([id='item'])").poshytip({
		content: 'Enter a number or skip it',
		className: 'tip-yellowsimple',
		showOn: 'focus',
		alignTo: 'target',
		alignX: 'inner-left',
		offsetX: 0,
		offsetY: 5
	});

    //限制输入为数字和"."
    $("#form1 input:not([id='item'])").numeric({allow:"."});
    $("#form3 input:not([id='item'])").numeric({allow:"."});
    
    //执行查询操作，并显示结果
    function PerformSearch()
    {
    	var url="searchProduct.action?"+$("#form1").serialize();
		$("#content").show();
		$("#content").dataTable({
			"bJQueryUI": true,
			"sPaginationType": "full_numbers",
			"bProcessing":true,
			"sAjaxSource":url,			
			"bDestroy":true,
			"sDom":'CT<"clear">R<"H"lfr>t<"F"ip>',
			"oTableTools":{
				"sSwfPath":"../../JavaScript/DataTables/extras/TableTools/media/swf/copy_cvs_xls_pdf.swf"
				},
			"oColVis": {
						"buttonText": "Change columns"
					}
		});
    }
    		   		    		
   	//查询Product信息
  	$("#search").click(function(){
  		var boolform1=true;
  		$("#form1 input").each(function(){
  			if($(this).val()!="")
  				boolform1=false;
  		});
		if($("#category").val()==""&&boolform1)
		{
			$( "#dialog-form4" ).dialog( "open" );
  			if ($.browser.msie) {           
  				$("#dialog-form4").css("height", 100);                
			} 
		}
		else
			PerformSearch();
  	}); 
  	
    //编辑商品信息
    $("#editOne").live("click", function(){
      var ID=$(this).attr("value");
      $.post("selectProduct.action",{id:ID},function(data){
      	data=eval("("+data+")");
      	$("#s_id").val(ID);
      	$("#s_item").val(data.item);
      	$("#s_category").val(data.category);
      	$("#s_weight").val(data.weight);
      	$("#s_volume").val(data.volume);
      	$("#s_height").val(data.height);
      	$("#s_width").val(data.width);
      	$("#s_depth").val(data.depth);
      	$("font").html("");
      	$("#d1").html("*");
      	$("#d2").html("*");
      });
    	$( "#dialog-form1" ).dialog( "open" );
    	if ($.browser.msie) {           
    		$("#dialog-form1").css("height", 300);                
  	}    	
    });
    
    //删除商品记录
    $("#deleteOne").live("click", function(){
      var ID=$(this).attr("value");
      $("#d_id").val(ID);
      $( "#dialog-form2" ).dialog( "open" );
      if($.browser.msie) {           
    	  $("#dialog-form2").css("height", 100);                
  	  } 
    });
  	
  	//勾选全部
  	$("#selectAll").click(function(){
  		var checkBool=false;
  		if($(this).attr("checked"))
  			checkBool=true;
  		$(".checkOne").each(function(){
  			$(this).attr("checked",checkBool);
  		});
  	});
  	
  	//删除多项
  	var deleteItems;
  	$("#delete").click(function(){
  		deleteItems="";
  		$(".checkOne").each(function(){
  			if($(this).attr("checked"))
  				deleteItems+=$(this).attr("value")+"#";
  		});
  		if(deleteItems=="")
  		{
  			alert("You have not select the items");
  			return;
  		}
  		deleteItems=deleteItems.substring(0,deleteItems.length-1);
  		var temp=deleteItems.split("#");
  		$("#dialog-form3 p").html(temp.length.toString()+" Products information will be deleted");
 		$( "#dialog-form3" ).dialog( "open" );
 		if ($.browser.msie) {           
  			$("#dialog-form3").css("height", 100);                
		}
  	});
  	
  	/*-----提示对话框 -----*/
  	//更新对话框	
  	$( "#dialog-form1" ).dialog({
			autoOpen: false,
			height: 450,
			width: 600,
			modal: true,			
			buttons: {
				"Save": function() {
					if($("#s_item").val()=="")
					{
						alert("The Item Name can't be empty");
						return;
					}
					$.post("checkPro.action",{
    					item:$("#s_item").val()
    				},function(data){
    					if(data=="error")
    					{
    						alert("Check Item Name Timeout,Please try again later");
    					}
    					else if(data=="false"||data==$("#s_id").val())
    					{
    						$("#d1").html("*");
    						$.post("updateProduct.action",$("#form3").serialize(),function(data){
							if(data=="true")
							{
								PerformSearch();
								$( "#dialog-form1" ).dialog( "close" );
							}
							else if(data=="false") alert("Update the product information failed");
							else alert("Lost connection to the database");					
							});		
    					}
    					else
    					{
    						alert("*This Product has already exists");
    					}
    				});
					$(".select").each(function(){
						if($(this).val()=="")
							$(this).val("0");
					});			
				},
				Cancel: function() {
					$( this ).dialog( "close" );
				}
			},
			close: function() {
				allFields.val( "" ).removeClass( "ui-state-error" );
			}
		});
		
		//删除对话框
		$( "#dialog-form2" ).dialog({
			autoOpen: false,
			height: 250,
			width: 500,
			modal: true,
			buttons: {
				"Continue": function() {
					$.post("delProduct.action",{id:$("#d_id").val()},function(data){
  						if(data=="false")
  							alert("Delete Failed");
  						else if(data=="error")
  							alert("Lost connection to the server");
  						PerformSearch();
  						$( "#dialog-form2" ).dialog( "close" );
  					});
				},
				Cancel: function() {
					$( this ).dialog( "close" );
				}
			},
				close: function() {
					allFields.val( "" ).removeClass( "ui-state-error" );
				}
		});
		
		//删除多项对话框
		$( "#dialog-form3" ).dialog({
			autoOpen: false,
			height: 250,
			width: 500,
			modal: true,
			buttons: {
				"Continue": function() {						
						$.post("delMoreProduct.action",{id:deleteItems},function(data){
  						if(data=="false")
  						{
  							alert("Delete Failed");
  							return;
  						}
  						else if(data=="error")
  						{
  							alert("Lost connection to the server");
							return;
						}
						PerformSearch();
  						$( "#dialog-form3" ).dialog( "close" );
						
  					});
  					
				},
				Cancel: function() {
					$( this ).dialog( "close" );
				}
			},
				close: function() {
					allFields.val( "" ).removeClass( "ui-state-error" );
				}
		});
		
		//无条件查询对话框
		$( "#dialog-form4" ).dialog({
			autoOpen: false,
			height: 250,
			width: 500,
			modal: true,
			buttons: {
				"Continue": function() {
					PerformSearch();
					$(this).dialog("close");
				},
				Cancel: function() {
					$( this ).dialog( "close" );
				}
			},
				close: function() {
					allFields.val( "" ).removeClass( "ui-state-error" );
				}
		});
  });